package luminus.origin.commands

import luminus.origin.commands.rewrite.Whisper.reply
import luminus.origin.utils.send
import org.bukkit.command.CommandSender
import taboolib.common.platform.command.*
import taboolib.common.platform.function.adaptPlayer
import taboolib.platform.util.asLangText
import taboolib.platform.util.sendLang

@CommandHeader("reply", ["r"], permission = "luminus.whisper.reply", permissionDefault = PermissionDefault.TRUE)
object Reply {
    @CommandBody
    val main = mainCommand {
        dynamic("message") {
            execute<CommandSender> { sender, context, _ ->
                val target = reply[adaptPlayer(sender)] ?: return@execute sender.sendLang("command.reply.unknownPlayer")
                val message = context.player("message")
                if (target.name == sender.name) {
                    sender.sendLang("commands-reply-errorTarget")
                } else {
                    sender.send(sender.asLangText("commands-reply-whisperSend").format(target.name, message))
                    target.send(sender.asLangText("commands-reply-whisperReceive").format(sender.name, message))
                }
            }
        }
    }
}